from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse

from .models import Student
from .forms import StudentForm


# Create your views here.
def index(request):
    students = Student.all()
    if request.method == 'POST':
        form = StudentForm(request.POST)
        if form.is_valid():
            print(1)
            form.save()
        else:
            print(2)
        return HttpResponseRedirect(reverse('index'))
    else:
        form = StudentForm()
    context = {
        'students': students,
        'form': form,
    }
    return render(request, 'index.html', context=context)